﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;

namespace PloobsEngine.SceneControl
{
    public class DephtOfFieldPostEffect : IPostEffect
    {


        #region IPostEffect Members
        BlurPostEffect be;
        SpriteBatch spriteBatch;
        Effect depht;
        EngineStuff engine;
        RenderTarget2D target;
        float range = 50f;
        float distance = 70f;

        public float Range
        {
            set
            {
                this.range = value;
            }
            get
            {
                return this.range;
            }
        }

        public float Distance
        {
            set
            {
                this.distance = value;
            }
            get
            {
                return this.distance;
            }
        }

        public void init(EngineStuff engine)
        {
            spriteBatch = EngineStuff.SpriteBatch;
            this.engine = engine;
            depht = EngineStuff.InternalContentManager.GetAsset<Effect>("depth");
            target = EngineStuff.GetDefaultColorBuffer();
            be = new BlurPostEffect();
            be.init(engine);
        }

        public void Draw(IRenderHelper rHelper, Microsoft.Xna.Framework.GameTime gt, Microsoft.Xna.Framework.Rectangle screenRec,IWorld world)
        {

            RenderTarget2D rd = EngineStuff.GraphicsDevice.GetRenderTarget(0) as RenderTarget2D;
            EngineStuff.GraphicsDevice.SetRenderTarget(0, target);

            be.Draw(rHelper,  gt, EngineStuff.GetFullScreenRectangle(), world);
            EngineStuff.GraphicsDevice.SetRenderTarget(0, rd);
                        
            spriteBatch.Begin(SpriteBlendMode.None, SpriteSortMode.Immediate, SaveStateMode.None);
            {
                depht.Begin();
                {
                    depht.CurrentTechnique.Passes[0].Begin();
                    {
                        depht.Parameters["BlurScene"].SetValue(target.GetTexture());
                        depht.Parameters["FarPlane"].SetValue(world.ActiveCamera.FarPlane);
                        depht.Parameters["D1M"].SetValue(rHelper[PrincipalConstants.dephRT]);
                        SetShaderParameters(distance, range, world.ActiveCamera.NearPlane, world.ActiveCamera.FarPlane);
                        spriteBatch.Draw(rHelper[PrincipalConstants.CurrentImage], screenRec, Color.White);
                     
                    }
                    depht.CurrentTechnique.Passes[0].End();
                }
                depht.End();
            }
            spriteBatch.End();

            

            
        }

        void SetShaderParameters(float fD, float fR, float nC, float fC)
        {
            float focusDistance = fD;
            float focusRange = fR;
            float nearClip = nC;
            float farClip = fC;
            farClip = farClip / (farClip - nearClip);

            depht.Parameters["Distance"].SetValue(focusDistance);
            depht.Parameters["Range"].SetValue(focusRange);
            depht.Parameters["Near"].SetValue(nearClip);
            depht.Parameters["Far"].SetValue(farClip);
        }

        #endregion
    }
}
